package com.exahq.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.exahq.common.lang.Result;
import com.exahq.entity.*;
import com.exahq.service.CompanydataService;
import com.exahq.service.PartnerinfoService;
import com.exahq.service.ViweService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhq
 * @since 2021-02-27
 */
@RestController
@RequestMapping("/companydata")
public class CompanydataController {
    @Autowired
    CompanydataService companydataService;
    @Autowired
    PartnerinfoService partnerinfoService;
    @Autowired
    ViweService viweService;
    /**
     * 企业数据查询
     * @param query
     * @param response
     * @return
     */
    @PostMapping("/companys")
    public Result company(@RequestBody Map<String,String> query, HttpServletResponse response) {
        SelectDataCompany selectData =new SelectDataCompany();
        Integer currentPage = Integer.valueOf(query.get("pageIndex"));
        selectData.setPageIndex(currentPage);
        if(currentPage == null || currentPage < 1) selectData.setPageIndex(0);
        selectData.setCompanyName(query.get("companyName"));
        selectData.setPartnerName(query.get("name"));
        selectData.setRegisterStatus(query.get("registerStatus"));
        if (!query.get("companytype").equals("") && query.get("companytype")!=null)
            selectData.setTypeId(Integer.valueOf(query.get("companytype")));
        if (!query.get("industry").equals("") && query.get("industry")!=null )
            selectData.setInId(Integer.valueOf(query.get("industry")));
        if (!query.get("organizations").equals("") && query.get("organizations")!=null)
            selectData.setOrId(Integer.valueOf(query.get("organizations")));
        selectData.setPageSize(Integer.valueOf(query.get("pageSize")));
        System.out.println(selectData.getPageIndex());
        List<ResultCompany> companyList =companydataService.getByQuery(selectData);
        int totalCount = companydataService.getCount(selectData);
        Map<Object,Object> map =new HashMap<Object,Object>();
        map.put("companyList",companyList);
        map.put("totalCount",totalCount);
        return Result.succ(map);
    }

    /**
     * 详情查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result detail(@PathVariable(name = "id") Integer id) {
        System.out.println(id);
        Companydata company = companydataService.getDetail(id);
        return Result.succ(company);
    }

    /***
     * 增加/修改
     * @param companydata
     * @return
     */
    @PostMapping("saveEdit")
    public Result saveEdit(@RequestBody Companydata companydata){
        if (companydata!=null && !companydata.equals("")){
            System.out.println(companydata);
            if (companydata.getCid() == null){
                companydata.setUpDate(new Date());
                boolean b = companydataService.save(companydata);
                if (b){
                    Viwe viwe =viweService.getById(1);
                    viwe.setCompanyCount(viwe.getCompanyCount()+1);
                    viweService.saveOrUpdate(viwe);
                    return Result.succ("新添成功",b);
                }
            }else {
                companydata.setUpDate(new Date());
                if (companydataService.saveOrUpdate(companydata))
                    return Result.succ("修改成功",companydataService.saveOrUpdate(companydata));
            }
        }
        return Result.succ(null);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/del/{id}")
    public Result del(@PathVariable(name = "id") Integer id) {
        return Result.succ( companydataService.delete(id));
    }

    /**
     * 修改数据查询
     * @param id
     * @return
     */
    @GetMapping("/editor/{id}")
    public Result editor(@PathVariable(name = "id") Integer id) {
        return Result.succ( companydataService.getById(id));
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/finaAll")
    public Result finaAll() {
        List<Companydata> companydataList =companydataService.list();
        return Result.succ(companydataList);
    }

    @GetMapping("/byName/{id}")
    public Result byName(@PathVariable(name = "id") String id) {
        QueryWrapper<Companydata> companydataQueryWrapper =new QueryWrapper<Companydata>();
        companydataQueryWrapper.eq("companyName",id);
        companydataQueryWrapper.eq("del_flag",1);
        List<Companydata> list =companydataService.list(companydataQueryWrapper);
        boolean b =true;
        if (list!=null){
            b =false;
        }
        return Result.succ(b);
    }

    /**
     * 股权分配
     * @param id
     * @return
     */
    @GetMapping("/companySelect/{id}")
    public Result companySelect(@PathVariable(name = "id") String id) {
        Companydata companydata =null;
        //总收益
        QueryWrapper<Partnerinfo> queryWrapper = new QueryWrapper<Partnerinfo>();
        queryWrapper.eq("cid",id);
        queryWrapper.eq("del_flag",1);
        queryWrapper.select("sum(RealCapi) as realCapital");
        Partnerinfo ord= partnerinfoService.getOne(queryWrapper);
        //注意，空指针问题
        if (ord == null){
            companydata =new Companydata();
        }else{
            companydata =companydataService.getById(id);
            if (ord.getRealCapi()< companydata.getPaidCapital()){
                companydata =new Companydata();
            }
        }
        return Result.succ(companydata);
    }

    /**
     * 数据计算
     * @param id
     * @return
     */
    @GetMapping("/numberSum/{id}")
    public Result numberSum(@PathVariable(name = "id") String id) {
        //总收益
        QueryWrapper<Partnerinfo> queryWrapper = new QueryWrapper<Partnerinfo>();
        queryWrapper.eq("cid",id);
        queryWrapper.eq("del_flag",1);
        queryWrapper.select("sum(RealCapi) as realCapi");
        Partnerinfo ord= partnerinfoService.getOne(queryWrapper);
        Companydata companydata = companydataService.getById(id);
        //注意，空指针问题
        if (ord != null){
            companydata.setRegisterCapital(companydata.getPaidCapital()-ord.getRealCapi());
        }
        return Result.succ(companydata);
    }
/**小程序数据操作**/
    /**
     *
     * @return
     */
    @GetMapping("/getData")
    public Result getData() {
        List<Companydata> dataByTimeList =companydataService.dataByTime();
        List<Companydata> dataNewList =companydataService.dataNew();
        Map<Object,Object> map =new HashMap<Object, Object>();
        map.put("dataNew",dataNewList);
        map.put("dataByTime",dataByTimeList);
        return Result.succ(map);
    }

    /**
     * 小程序多条件查询
     * @param companyData
     * @return
     */
    @RequestMapping(value = "selectCompanyData",method = RequestMethod.POST)
    @PostMapping("/selectCompanyData")
    public Result selectCompanyData(@RequestBody SelectDataCompany companyData) {
        //JSONObject json_test = JSONObject.fromObject(companyData);
        //json_test.get("companyName").toString()
        List<Companydata> list =companydataService.selectCompanyData(companyData);
        Map<Object,Object> map =new HashMap<Object, Object>();
        map.put("list",list);
        map.put("count",list.size());
        return Result.succ(map);
    }

    /**
     * 企业查询
     * @param companyId
     * @return
     */
    @PostMapping("/selectCompanyInfo")
    public Result selectCompanyInfo(@RequestBody String companyId) {
        JSONObject json_test = JSONObject.fromObject(companyId);
        Companydata companydata =companydataService.getDetail(Integer.valueOf(json_test.get("companyId").toString()));
        List<Partnerinfo> list = partnerinfoService.byCidData(Integer.valueOf(json_test.get("companyId").toString()));
        Map<Object,Object> map =new HashMap<Object, Object>();
        map.put("list",list);
        map.put("companydata",companydata);
        return Result.succ(map);
    }

    /**
     * 工商信息
     * @param cid
     * @return
     */
    @PostMapping("/selectInfo")
    public Result selectInfo(@RequestBody String cid) {
        System.out.println(cid);
        JSONObject json_test = JSONObject.fromObject(cid);
       Companydata company = companydataService.getDetail(Integer.valueOf(json_test.get("cid").toString()));
        return Result.succ(company);
    }
}
